﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using XFControls.XFPanels;
using FBEntities;
using XFControls.XFPanels.XFPanelItems;
using FBEntities.FBObjects;
using FBEntities.FBObjects.User;
using FBEntities.FBConnections.User;
using XFItemControls.XFPanels.XFPanelItems;

namespace XFControls
{
    public class XFItemControlUtils
    {
        public delegate void AddFriendsDel(XFPanelList panel, FBDataSource ds);

        public static FBDataSource Me { get; set; }

        public static void AddFriends(XFPanelList panel, FBDataSource ds)
        {
            if (panel.InvokeRequired)
            {
                if (!panel.IsDisposed)
                    panel.Invoke(new AddFriendsDel(AddFriends), panel, ds);
                return;
            }

            if (ds.Me.Friends.data.Count == 0)
            {
                var empty = new XFItemSimpleText("You have no friends :'(");
                panel.Add(empty);
                panel.ScrollbarColor = Color.LightSlateGray;
                return;
            }


            var friends = ds.Me.Friends.data;
            friends.Sort(new Comparison<FBProxyPerson>(FriendCompare));
            char[] curLetter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
            int cur = 0;
            panel.Add(curLetter[cur].ToString());

            foreach (var friend in friends)
            {
                string fLast = friend.name.Split(' ').Last();

                if (!fLast.StartsWith(curLetter[cur].ToString()))
                {
                    int tempCur = Array.IndexOf(curLetter, fLast[0]);
                    if (tempCur > 0)
                    {
                        cur = tempCur;
                        panel.Add(curLetter[cur].ToString());
                    }
                }

                var f = new XFItemFriend();
                f.Name = friend.name;
                f.Friend = friend;
                panel.Add(f);
            }
        }

        private static int FriendCompare(FBProxyPerson f1, FBProxyPerson f2)
        {
            string f1Last = f1.name.Split(' ').Last();
            string f2Last = f2.name.Split(' ').Last();

            return f1Last.CompareTo(f2Last);
        }

        public static XFPanelList SetupProfile(FBPerson pers)
        {
            XFPanelList panel = new XFPanelList();
            panel.ScrollbarColor = Color.LightSlateGray;

            var worker = new XFWorkerLoadProfile(pers);
            var loading = new XFItemLoading(worker, false);

            loading.WorkFinished += (o, e) =>
            {
                if (!panel.IsDisposed)
                    panel.Invoke(new Action(() =>
                        {
                            panel.ClearItem(panel[loading]);
                        }));
            };

            panel.Add(loading);

            FBPerson cachedPerson = pers;

            XFItemProfileHead headCache = new XFItemProfileHead();
            headCache.User = cachedPerson;

            XFItemProfileTabs tabsCache = new XFItemProfileTabs();
            tabsCache.User = cachedPerson;

            XFItemProfileInfo itmCache = new XFItemProfileInfo();
            itmCache.User = cachedPerson;

            panel.Add(headCache);
            panel.Add(tabsCache);
            panel.Add(itmCache);

            loading.DoWork();
            return panel;
        }

        internal static void AddMailItems(XFPanelList panel, FBDataSource dsInner)
        {
            if (panel.InvokeRequired)
            {
                if (!panel.IsDisposed)
                    panel.Invoke(new Action(() => { AddMailItems(panel, dsInner); }));
                return;
            }

            panel.ClearItems();
            panel.Add(new XFItemComposeMessage());

            foreach (var item in dsInner.Me.Inbox.data)
            {
                panel.Add(new XFItemInboxMessage(item));
            }
        }
    }
}
